<?php
/*
 * @Author: 莫莫
 * @Phone: 13450159681
 * @Description: 
 * @Date: 2024-10-22 17:06:17
 * @LastEditTime: 2024-11-20 11:11:50
 */

namespace app\model;

use think\Model;

class Openid extends Model
{

	protected $connection = 'mysql';

	protected $pk = 'id';

	protected $name = 'openid';
	public static function getMpOpenidbyuid($uid)
	{
		$data = self::where(['weid' => weid(), 'uid' => $uid, 'ptype' => 'mp'])->order('id desc')->find();
		if (!empty($data)) {
			return $data->openid;
		}
	}
	public static function getMpOpenidbyuuid($uuid)
	{
		$query = self::where(['weid' => weid(), 'uuid' => $uuid, 'ptype' => 'mp']);
		$MP = $query->order('id desc')->find();

		if (empty($MP)) {
			$uid = Member::get_uidByuuid($uuid);
			if (!empty($uid)) {
				return self::getMpOpenidbyuid($uid);
			}
		}

		if (!empty($MP)) {
			return $MP->openid;
		}
	}
	public static function getWxappOpenidbyuid($uid)
	{
		$data = self::where(['weid' => weid(), 'uid' => $uid, 'ptype' => 'wxapp'])->order('id desc')->find();
		if (!empty($data)) {
			return $data->openid;
		}
	}
	public static function getuidbyopenid($openid = '')
	{
		if (empty($openid)) {
			$openid = getFans()['openid'];
		}
		$query = self::where(['weid' => weid(), 'openid' => $openid]);
		$query->where('uid', '>', 0);
		$mob = $query->order('id desc')->find();
		if (!empty($mob)) {
			if (empty($mob->ptype)) {
				$mob->ptype = getFans()['ptype'];
				$mob->save();
			}
			return $mob->uid;
		}
	}

	public static function getuuid($ptype)
	{
		$query = self::where(['weid' => weid(), 'openid' => getFans()['openid'], 'ptype' => $ptype, 'status' => 0]);
		$query->whereNotNull('uuid');
		$mob = $query->order('id desc')->find();

		if (!empty($mob)) {
			return $mob->uuid;
		}
	}
	public static function getadminuuid()
	{
		$query = self::where(['weid' => weid(), 'openid' => getFans()['openid'], 'ptype' => 'admin']);
		$query->whereNotNull('uuid');
		$mob = $query->order('id desc')->find();

		if (!empty($mob)) {
			return $mob->uuid;
		}
	}
}
